<?php

class Admin_PageController extends Zend_Controller_Action
{
    protected $_em;
    protected $_repo;
    protected $_flash;
    
    public function init()
    {
        $this->_em = Zend_Registry::get('doctrine');
        $this->_flash = $this->_helper->getHelper('FlashMessenger');
    }

	public function indexAction() {
	    $db_page = new  Admin_Model_DbTable_page();
		$this->view->manager = 'MANAGEMENT PAGE LIST';
        $this->view->page = $db_page->fetchAll();
	}
	
	////////////////// Country ///////////////////////
	 
	public function pageAction(){
		
		$db_page = new  Admin_Model_DbTable_page();
		$this->view->manager = 'MANAGEMENT PAGE LIST';
        $this->view->page = $db_page->fetchAll();   
	}
	public function editpageAction(){
		
		$id = $this->_getParam('id', 0);
        
		$form = new Admin_Form_Page();
		$this->view->form = $form;
		
		$this->_repo = $this->_em->getRepository('Entities\HaePage');
		$data = $this->_repo->findOneById($id);
		$this->_repo = $this->_em->getRepository('Entities\HaeSeo');
		
		
		
		if ( $data )
		{
		    $this->view->manager = 'EDITOR  PAGE :'.$data->getTitle();
		    
		    $dataSeo = $this->_repo->findOneById($data->getseoId());
		    
		    $form->populate($data->toArray());
		    
		    $form->populate($dataSeo->toArray());
		   
		    if($this->getRequest()->isPost())
		    {
		        
		    	if ($form->isValid($this->getRequest()->getPost()))
		    	{
		    		$this->_savePage($this->getRequest()->getPost());
		    		$this->_helper->flashMessenger('Edit successfully');
		    	}
		    	else
		    	{
		    		$codes = $form->getErrors();
		    		$messages = $form->getMessages();
		    		$this->_helper->flashMessenger('Vous devez corriger les erreurs');
		    		$this->_helper->flashMessenger(var_dump($messages));
		    		$this->_helper->flashMessenger(var_dump($codes));
		    	}
		    }
		}
		else 
		{
		    throw new Zend_Controller_Action_Exception('Page not found',500);
		}
	}
	
	public function _savePage($postArray)
	{
	    $obj = (object) $postArray;
	    $id = $this->_getParam('id', 0);
	    $this->_repo = $this->_em->getRepository('Entities\HaePage');
	    $page = $this->_repo->findOneById($id);
	    
	    $page->setTitle($obj->title);
	    $page->setDescVi($obj->descVi);
	    $page->setDescEn($obj->descEn);
	    $page->setDescJa($obj->descJa);
	    $page->setDescZh($obj->descZh);
	    $page->setDescFr($obj->descFr);
	    $page->setSeoId($this->_saveSeo($page->getSeoId(),$postArray));
	    
	    $this->_em->persist($page);
	    $this->_em->flush();
	}
	public function _saveSeo($id = 0,$postArray)
	{
	    $obj = (object) $postArray;
	    
	    $this->_repo = $this->_em->getRepository('Entities\HaeSeo');
	    if ($id==0)
	    {
	    	$seo = new Entities\HaeSeo;
	    }
	    else 
	    {
	        $seo = $this->_repo->findOneById($id);
	    }
	    
	    // Set Alias For Seo 
	    $seo->setAliasVi($this->_helper->rewrite($obj->title));
	    $seo->setAliasEn($this->_helper->rewrite($obj->title));
	    $seo->setAliasJa($this->_helper->rewrite($obj->title));
	    $seo->setAliasZh($this->_helper->rewrite($obj->title));
	    $seo->setAliasFr($this->_helper->rewrite($obj->title));
	    // Set Metatag for Seo
	    $seo->setMetatagVi($obj->metatagVi);
	    $seo->setMetatagEn($obj->metatagEn);
	    $seo->setMetatagJa($obj->metatagJa);
	    $seo->setMetatagZh($obj->metatagZh);
	    $seo->setMetatagFr($obj->metatagFr);
	    //Set keyword for Seo
	    $seo->setKeywordVi($obj->keywordVi);
	    $seo->setKeywordEn($obj->keywordEn);
	    $seo->setKeywordJa($obj->keywordJa);
	    $seo->setKeywordZh($obj->keywordZh);
	    $seo->setKeywordFr($obj->keywordFr);
	    
	    $this->_em->persist($seo);
	    $this->_em->flush();
	    return $seo->getId();
	}
}
?>